﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public string convertoneword(Match m)
        {
            Regex r = new Regex(@"[\da-f]{4}", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            string str = "";
            string sss = m.ToString();
            sss = r.Match(m.ToString()).Value;
            str += Convert.ToChar(Convert.ToInt32(r.Match(m.ToString()).Value, 16));
            return str;
        }

        public string convertutf8tohz(Match m)
        {
            // Get the matched string.

            string str = m.ToString();
            // If the first char is lower case...

            string sr =
@"\\u([\da-f]{4})";

            Regex r = new Regex(sr, RegexOptions.Singleline|RegexOptions.IgnoreCase);
            MatchCollection mc;
            mc = r.Matches(str);

            if (mc.Count <= 0)
                return str;

            str=r.Replace(str, new MatchEvaluator(convertoneword));

            return str;
        }

        private string convertline(string str)
        {
            string sr =
                @"\"".*\""";
            Regex r = new Regex(sr, RegexOptions.Singleline);
            MatchCollection mc;
            mc = r.Matches(str);

            if (mc.Count <= 0)
                return str;

            str=r.Replace(str, new MatchEvaluator(convertutf8tohz));

            return str;
        }


        private void buttonConvert_Click(object sender, RoutedEventArgs e)
        {

            string sr =
                //@"\""(.*)\""";
"(^.*$)";
            string str = textBox1.Text;
            Regex r = new Regex(sr, RegexOptions.Multiline);
            MatchCollection mc;
            mc = r.Matches(str);

            string strhz;
            textBox2.Text="";
            for (int i = 0; i < mc.Count; i++)
            {
                strhz = convertline(mc[i].Groups[1].Value);
                if (strhz != null)
                {
                    textBox2.Text += strhz;
//                    textBox2.Text += "\r\n";
                }
            }

        }
    }
}
